//
// Created by LiuYou on 2021/5/12.
//

/*
 * 操作
 *     begin()
 *     end()
 *     cbegin() 返回的应该是const元素。
 *     cend()
 *     rbegin()
 *     rend()
 *     crbegin()
 *     crend()
 *
 *     insert() 注意 频繁插入vector中间位置的元素的话, 那也使用 list容器。
 *     erase() 注意 频繁删除vector中间位置的元素的话, 那就使用 list容器。
 *     resize()
 *     clear()
 *
 *
 */
#include <vector>
#include <iostream>
#include "Book.hpp"


void test_insert();

void test_erase_function();

void test_resize();



int main() {
//    test_insert();

//    test_erase_function();

//    test_resize();

    std::vector<Book> vec;
    Book book1("1", "留攸");
    Book book2("2", "留攸");
    Book book3("3", "留攸");
    Book book4("4", "留攸");
    vec.push_back(book1);
    vec.push_back(book2);
    vec.push_back(book3);
    vec.push_back(book4);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }
    vec.reserve(100);
    // todo 注意: clear之后, 原来的vec的size()为0, capacity()的值不变！！！！
    vec.clear();
    std::cout << std::endl;
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }
    // 运行结果
//    /home/lucas/CLionProjects/CppStandardLibrary-Linux/cmake-build-debug/VectorTest04
//    vec.capacity(): 4
//    vec.size(): 4
//    name: 1 author: 留攸
//    name: 2 author: 留攸
//    name: 3 author: 留攸
//    name: 4 author: 留攸
//
//    vec.capacity(): 100
//    vec.size(): 0
//
//    Process finished with exit code 0



    return 0;
}

void test_resize() {
    std::vector<Book> vec;
    Book book1("1", "留攸");
    Book book2("2", "留攸");
    Book book3("3", "留攸");
    Book book4("4", "留攸");
    vec.push_back(book1);
    vec.push_back(book2);
    vec.push_back(book3);
    vec.push_back(book4);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;

    // TODO 注意: resize不仅改变了 size()的大小, capacity()也一样改变了而且 size()和capacity()的值跟resize()的实参相同。
    vec.resize(8);
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    // 运行结果:
//    /home/lucas/CLionProjects/CppStandardLibrary-Linux/cmake-build-debug/VectorTest04
//    vec.capacity(): 4
//    vec.size(): 4
//    name: 1 author: 留攸
//    name: 2 author: 留攸
//    name: 3 author: 留攸
//    name: 4 author: 留攸
//    name:  author:
//    name:  author:
//    name:  author:
//    name:  author:
//    vec.capacity(): 8
//    vec.size(): 8
//
//    Process finished with exit code 0


}

void test_erase_function() {
    std::vector<Book> vec;
    Book book = Book("moon", "留攸");
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    vec.push_back(book);
    Book book1 = Book("1", "1");
    Book book2 = Book("2", "2");
    Book book3 = Book("3", "3");
    std::vector<Book> vector{book1, book2, book3};
    vec.push_back(book1);
    vec.push_back(book2);
    vec.push_back(book3);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }

    std::cout << "===============" << std::endl;
    // 删除 vec[3]位置的元素, 返回值为删除vec[3]后, vec元素位置重整后的vec[3]位置的元素。
    std::cout << *vec.erase(vec.cbegin() + 3) << std::endl;
    std::cout << "===============" << std::endl;
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }
}

void test_insert() {
    std::vector<Book> vec;
    Book book = Book("moon", "留攸");
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;
    vec.push_back(book);
    std::cout << "vec.capacity(): " << vec.capacity() << std::endl;
    std::cout << "vec.size(): " << vec.size() << std::endl;

    Book book1("1", "1");
    Book book2("2", "2");
    Book book3("3", "3");
    std::vector<Book> vector{book1, book2, book3};
    // 在末尾追加
    vec.insert(vec.cend(), vector.cbegin(), vector.cend());
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }
    std::cout << std::endl;

    // 在中间位置添加
// 最后一个参数有问题!
// 解决办法, 将这个范围迭代器全部改为返回non-const iterator或者统一是返回const iterator, 使其统一, 不然会报错。
// 下面这四条语句都是正确的。
//    vec.insert(vec.begin() + 1, vector.begin(), vector.end());
//    vec.insert(vec.begin() + 1, vector.begin(), vector.end()-1);
//    vec.insert(vec.begin() + 1, vector.begin(), vector.begin() + 2);
    vec.insert(vec.cbegin() + 1, vector.cbegin(), vector.cbegin() + 2);
    // TODO 注意: vector.begin() + 2 其实就是 &vector[2]！！！！
    for (const auto& item : vec) {
        std::cout << item << std::endl;
    }
}
